package org.ttitfly.workflow.engine;

import java.util.List;

import org.ttitfly.workflow.exception.ProcessRunException;
import org.ttitfly.workflow.handler.Handler;


/**
 * 自动任务运行单元，当流程调度后无需人工干预即可运行完成，并负责触发流程调度下一个运行单元。
 * 
 * @author 潘海春
 * @version 2009-9-11
 */
public class AutoRunUnit extends TaskRunUnit {

	/**
	 * action动作
	 */
	private Action action;

	/**
	 * 构造函数
	 * 
	 * @param parent
	 * @param name
	 * @param action
	 */
	public AutoRunUnit(Process parent, String name, Action action) {
		this(parent, name, action, null, null);
	}

	/**
	 * 构造函数
	 * 
	 * @param parent
	 * @param name
	 * @param action
	 * @param activeHandlers
	 * @param closeHandlers
	 */
	public AutoRunUnit(Process parent, String name, Action action,
			List<Handler> activeHandlers, List<Handler> closeHandlers) {
		super(parent, name, activeHandlers, closeHandlers);
		this.action = action;
	}

	@Override
	public void run() throws ProcessRunException {
		super.run();
		if (action != null) {
			String nextUnitName = action.execute();
			close();
			if (nextUnitName == null) {
				parent.checkComplete();
			} else {
				parent.dispatch(nextUnitName);
			}
		}
	}

}
